//
//  HDCFModifyNameVC.m
//  HaoDeCaiFu
//
//  Created by admin on 2017/11/21.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFModifyNameVC.h"

@interface HDCFModifyNameVC ()<UITextFieldDelegate>
{
    UITextField * nameTextField;
}
@end

@implementation HDCFModifyNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"修改姓名"];
    self.view.backgroundColor = ZKcolor(243, 243, 243);
    
    [self initView];
    //[self customNavigationRightButtonTitle:@"确定"];
    // Do any additional setup after loading the view.
}
-(void)initView
{
    UIView * backView = [[UIView alloc]init];
    backView.backgroundColor = [UIColor whiteColor];
    backView.frame = CGRectMake(0, KNavHeight, screenWidth, 50);
    [self.view addSubview:backView];
    
    
    UIImageView * headerImageView = [[UIImageView alloc]init];
    headerImageView.frame = CGRectMake(15, 16, 20, 18);
    headerImageView.image = [UIImage imageNamed:@"icon_name"];
    [backView addSubview:headerImageView];
    
    nameTextField = [[UITextField alloc]init];
    nameTextField.frame = CGRectMake(headerImageView.right +10, 10, screenWidth - 15*2 - 30, 30);
    nameTextField.placeholder = @"请输入姓名";
    nameTextField.borderStyle = UITextBorderStyleRoundedRect;
    nameTextField.font = [UIFont systemFontOfSize:14.0];
    nameTextField.delegate = self;
    [backView addSubview:nameTextField];
    
    UIButton * submitButton = [[UIButton alloc]init];
    submitButton.frame = CGRectMake(40, backView.bottom +20, screenWidth- 40*2, 35);
    [submitButton setTitle:@"提交" forState:UIControlStateNormal];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
    submitButton.backgroundColor = COLOR_NAV;
    submitButton.layer.masksToBounds = YES;
    submitButton.layer.cornerRadius = 5;
    submitButton.titleLabel.textColor = [UIColor whiteColor];
    [submitButton addTarget:self action:@selector(rightBtnTop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitButton];
}

//导航栏右边是文字
-(void)customNavigationRightButtonTitle:(NSString *)string
{
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] init];
    
    UILabel * textLabel = [[UILabel alloc]init];
    textLabel.frame = CGRectMake(0, 5, 100, 30);
    textLabel.text = string;
    textLabel.textColor = [UIColor whiteColor];
    textLabel.font = [UIFont systemFontOfSize:15.0];
    textLabel.textAlignment = NSTextAlignmentRight;
    
    rightBtn.customView = textLabel;
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    textLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *popTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightBtnTop)];
    [textLabel addGestureRecognizer:popTap];
}
-(void)rightBtnTop
{
    [nameTextField resignFirstResponder];
    if (nameTextField.text.length ==0) {
        [SVProgressHUD showInfoWithStatus:@"请输入姓名"];
        return;
    }
    NSDictionary * dict = @{@"Name":nameTextField.text};
    NSString *jsonString = [self dictionaryToJson:dict];
    [HttpRequest sendSelfPostRequestWithURL:@"/api/User/Edit" WithParamets:jsonString WithParamet:nil success:^(id result) {
        if ([result[@"code"] isEqual:@1]) {
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            [SVProgressHUD dismissWithDelay:1.0];
            if ([_delegate respondsToSelector:@selector(returnHDCFModifyName:)]) {
                [_delegate returnHDCFModifyName:nameTextField.text];
            }
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}
- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSString *jsonTemp = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return jsonTemp;
}
#pragma mark -
#pragma mark UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    nameTextField.text = textField.text;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
